package com.factory.iamp.pojo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.utils.exception.APIException;
import com.factory.iamp.utils.helper.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ArchiveDataLog {
    private int lid;
    private String table_count;
    private String problem_count;
    private String problem_class_count;
    private String created_at;

    private boolean isTableCountNum() {
        return this.table_count.chars().allMatch(Character::isDigit);
    }

    private int getTC(int line) {
        if(this.isTableCountNum()) {
            if(line == 1 || line == -1) return Integer.parseInt(this.table_count); // TODO: 确认常量数值
        } else {
            JSONObject tc_data = JSONObject.parseObject(this.table_count);
            if(line == -1) return tc_data.getIntValue("total");
            if(tc_data.containsKey(String.valueOf(line))) return tc_data.getIntValue(String.valueOf(line));
        }
        return 0;
    }

    public int getTable_count() {
        return getTC(-1);
    }

    public int getTable_count(int line) {
        return getTC(line);
    }

    private String getPCC(String key) {
        JSONObject pcc_data = JSONObject.parseObject(this.problem_class_count);
        if(pcc_data.containsKey("total")) return pcc_data.getString(key);
        if(key.equals("1")) return pcc_data.toJSONString(); // TODO: 确认常量数值
        else return "{}";
    }

    public String getProblem_class_count() {
        return getPCC("total");
    }

    public String getProblem_class_count(int line) {
        return getPCC(String.valueOf(line));
    }

    private boolean isProblemCountNum() {
        return this.problem_count.chars().allMatch(Character::isDigit);
    }

    private int getPC(int line) {
        if(this.isProblemCountNum()) {
            if(line == 1 || line == -1) return Integer.parseInt(this.problem_count); // TODO: 确认常量数值
        } else {
            JSONObject pc_data = JSONObject.parseObject(this.problem_count);
            if(line == -1) return pc_data.getIntValue("total");
            if(pc_data.containsKey(String.valueOf(line))) return pc_data.getIntValue(String.valueOf(line));
        }
        return 0;
    }

    public int getProblem_count() {
        return getPC(-1);
    }

    public int getProblem_count(int line) {
        return getPC(line);
    }

    public void addTable(Table table) {
        JSONObject tc_data, pc_data, pcc_data;
        String TenKV = "1"; // TODO: 确认常量数值
        String LID = String.valueOf(table.getLid());

        if(this.isTableCountNum()) {
            tc_data = new JSONObject();
            tc_data.put("total", this.table_count);
            tc_data.put(TenKV, this.table_count);
        } else tc_data = JSONObject.parseObject(this.table_count);
        tc_data.put("total", tc_data.getIntValue("total") + 1);
        if(tc_data.containsKey(LID)) tc_data.put(LID, tc_data.getIntValue(LID) + 1);
        else tc_data.put(LID, 1);
//        this.table_count += 1;
        if(isProblemCountNum()) {
            pc_data = new JSONObject();
            pc_data.put("total", this.table_count);
            pc_data.put(TenKV, this.problem_count);
        } else pc_data = JSONObject.parseObject(this.problem_count);
        if(!pc_data.containsKey(LID)) pc_data.put(LID, 0);

        pcc_data = JSONObject.parseObject(this.problem_class_count);
        if(!pcc_data.containsKey("total")) {
            JSONObject tmp = new JSONObject();
            tmp.put("total", pcc_data);
            tmp.put(TenKV, pcc_data);
            pcc_data = tmp;
        }

        JSONObject pcc1 = pcc_data.getJSONObject("total"), pcc2 = pcc_data.getJSONObject(LID);
        JSONArray problems = JSONArray.parseArray(table.getProblem());
        for(Object p: problems) {
            if(!((JSONObject)p).containsKey("list"))
                throw new APIException(ResponseCode.DATABASE_FAILED, "不存在问题列表项");
            JSONArray problemList = ((JSONObject)p).getJSONArray("list");
            pc_data.put("total", pc_data.getIntValue("total") + problemList.size());
            pc_data.put(LID, pc_data.getIntValue(LID) +problemList.size());
//            this.problem_count += problemList.size();

            for(Object pid: problemList) {
                String pids = String.valueOf(pid);
                if(!pcc1.containsKey(pids)) pcc1.put(pids, 0);
                pcc1.put(pids, pcc1.getInteger(pids) + 1);
                if(!pcc2.containsKey(pids)) pcc2.put(pids, 0);
                pcc2.put(pids, pcc2.getInteger(pids) + 1);
            }
        }
        pcc_data.put("total", pcc1);
        pcc_data.put(LID, pcc2);

        this.table_count = tc_data.toJSONString();
        this.problem_count = pc_data.toJSONString();
        this.problem_class_count = pcc_data.toJSONString();
    }
}
